using System;

namespace QntPlatform.Strategy.S2
{
    public class TrailingProfitAction : SActionBase
    {
        /// 止盈回撤率
        public decimal ProfitSlideRate { get; set; }
        /// 最小止盈率
        public decimal MinProfitRate { get; set; }
        //购入价
        public decimal InPrice { get; private set; }
        //历史最高盈利率
        public decimal TopProfitRate { get; private set; }
        public bool IsLong { get; private set; }
        public TrailingProfitAction(decimal inPrice, bool isLong, decimal _profitSlideRate = 0.001m, decimal _profitRate = 0.005m,ILog log=null)
        {
            ProfitSlideRate = _profitSlideRate;
            MinProfitRate = _profitRate;
            InPrice = inPrice;
            //TopProfitRate = topProfitRate;
            IsLong = isLong;
            this.log=log;
        }

        ILog log;
        /// 滑动止盈
        public bool? Execute(Ticker tck)
        {
            if (IsDisposable)
                return null;
            var nowRate = ISysFun.CalcProfitRate(tck.Last, InPrice, IsLong);
            if (nowRate > TopProfitRate)
            {
                TopProfitRate = nowRate;
                return FalseReturn();
            }
            if (TopProfitRate < MinProfitRate)
                return FalseReturn();
            if ((TopProfitRate - nowRate) >= TopProfitRate * ProfitSlideRate)
            {
                log.Debug("跟踪止盈运算返回ture", new { TopProfitRate, nowRate, tck.Last });
                return TrueReturn();
            }
            return FalseReturn();
        }
    }
}